from django.shortcuts import render,redirect,reverse
from django.http import  HttpResponse
from .models import *
# Create your views here.
def account(request):
    if request.method == 'GET':
        return render(request,'account.html')
    else:
        name = request.POST.get('uname')

        pwd = request.POST.get('upwd')
        user = Accounts.objects.filter(A_name=name,A_pwd=pwd)
        if user:
            return redirect(reverse('index'))
        else:
            return render(request, 'account.html')

def add(request):
    if request.method =="GET":
        all_S = Sources.objects.all()
        all_A = Accounts.objects.all()
        return render(request,'add.html',{'all_s':all_S,'all_c':all_A})
    else:
        name = request.POST.get('uname')
        phone = request.POST.get('uphone')
        Sourcess = request.POST.get('sources')
        Account = request.POST.get('Accounts')
        adds = Clients(C_name=name ,C_phone=phone,S_id_id=Sourcess,A_id_id=Account)
        adds.save()
        print('dsdas')
        return redirect(reverse('index'))



def index(request):
    if request.method == 'GET':
        all_user = Clients.objects.all()
        return render(request,'index.html',{'all_user':all_user})


def edit(request,id):
    if request.method =="GET":
        all_S = Sources.objects.all()
        all_C = Clients.objects.get(pk=id)
        return render(request,'edit.html',{'all_c':all_C,'all_s':all_S})
    else:
        name = request.POST.get('uname')
        phone = request.POST.get('uphone')
        Sourcess = request.POST.get('sources')
        d= Clients.objects.filter(pk=id).update(C_name=name ,C_phone=phone,S_id_id=Sourcess)
        return redirect(reverse('index'))
def delete(request, id):
    de = Clients.objects.get(pk=id)
    de.delete()
    return redirect(reverse('index'))